import {
    reqAddress, reqCategorys, reqShops, reqUserInfo,
    reqLogout, reqShopGoods,
    reqShopRatings,
    reqShopInfo
} from '../api'
import {
    RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS, RECEIVE_USER_INFO, RESET_USER_INFO, RECEIVE_GOODS,
    RECEIVE_RATINGS,
    RECEIVE_INFO
} from './mutation-types'


export default {
    // 异步获取地址
    async getAddress({ commit, state }) {
        const geohash = state.latitude + ',' + state.longitude
        const result = await reqAddress(geohash)
        commit(RECEIVE_ADDRESS, { address: result.data })
        // console.log(state.latitude);
        // console.log(result.data.latitude);
        // console.log(result.data.longitude);



    },


    // 异步获取分类列表
    async getCategorys({ commit }) {
        const result = await reqCategorys()
        commit(RECEIVE_CATEGORYS, { categorys: result.data })
    },


    // 异步获取商家列表
    async getShops({ commit, state }) {
        const { latitude, longitude } = state
        const result = await reqShops({ latitude, longitude })
        commit(RECEIVE_SHOPS, { shops: result.data })
    },

    //获取个人信息
    recordUser({ commit }, user) {
        console.log(user);

        commit(RECEIVE_USER_INFO, { userInfo: user })

    },
    //异步 获取用户会话信息 
    async  getUserInfo({ commit }) {
        let result = await reqUserInfo()
        if (result.code == 0) {
            let user = result.data
            commit(RECEIVE_USER_INFO, { userInfo: user })

        }
    },


    // 异步退出用户登录 
    async   getLogout({ commit }) {


        let result = await reqLogout()

        if (result.code == 0) {
            commit(RESET_USER_INFO)

        }
    },
    //异步获取商家商品信息 
    async   getShopGoods({ commit }) {
        let result = await reqShopGoods()
        if (result.code == 0) {
            let goods = result.data
            commit(RECEIVE_GOODS, { goods })
        }
    },

    //异步获取商家个人信息
    async   getShopInfo({ commit }) {
        let result = await reqShopInfo()
        if (result.code == 0) {
            let info = result.data
            commit(RECEIVE_INFO, { info })
        }
    },
    //异步获取商家商品评价
    async   getShopRatings({ commit }) {
        let result = await reqShopRatings()
        if (result.code == 0) {
            let ratings = result.data
            commit(RECEIVE_RATINGS, { ratings })
        }
    },

}